package com.yunhe.spring.mybatis.service.impl;

import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yunhe.spring.mybatis.dao.GoodsMapper;
import com.yunhe.spring.mybatis.model.Goods;
import com.yunhe.spring.mybatis.model.Pages;
import com.yunhe.spring.mybatis.service.IGoodsService;



@Service("goodsService")
public class GoodsServiceImpl implements IGoodsService {
	 private static Logger logger = LogManager.getLogger(GoodsServiceImpl.class);

	@Autowired
	private GoodsMapper goodsMapper;

	// 根据页面大小，当前页，模糊商品名字查询出商品信息放入Pages对象,处理相应业务
	@Override
	public Pages getAllGoods(Map<String, Object> map) {
		Pages pages = new Pages();
		pages.setTotalNum(goodsMapper.getAllGoodsCount(map));
		Integer curPageNum = Integer.parseInt(String.valueOf(map.get("curPageNum")));
		pages.setCurPageNum(curPageNum);
		Integer pageSize = Integer.parseInt(String.valueOf(map.get("pageSize")));
		pages.setPageSize(pageSize);
		pages.setTotalPage(pages.getTotalNum() / pageSize + (pages.getTotalNum() % pageSize == 0 ? 0 : 1));
		pages.setResult(goodsMapper.getAllGoods(map));
		return pages;
	}

	// 添加商品
	@Override
	public void addGoods(Goods goods) {
		logger.info("添加商品"+goods);
		goodsMapper.addGoods(goods);
	}

	// 删除商品
	@Override
	public void delGoods(Goods goods) {
		logger.info("删除商品"+goods);
		goodsMapper.delGoods(goods);
	}

	// 查找单个商品--用作修改商品
	@Override
	public Goods getGoodsOne(Goods goods) {
		// TODO Auto-generated method stub
		return goodsMapper.getGoodsOne(goods);
	}

	// 修改对应商品
	@Override
	public void editGoods(Goods goods) {
		logger.info("修改对应商品"+goods);
		goodsMapper.editGoods(goods);
	}

}
